<html>
<?php
/**
 * $Id: setup.php 10 2011-05-31 00:14:48Z bordjukov@gmail.com $
 */

system("/usr/bin/id");

########## ##########  Start Global Functions  ########## ##########
function curPageURL() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 $pos = strrpos($pageURL, "/");
 $pageURL = substr($pageURL, 0, $pos + 1);
 
 return $pageURL;
}
########## ##########  END Global Functions  ########## ##########

########## ##########  Start INSTALL  ########## ##########
	$applicationName = "tnt";
	// first open the $applicationName.xml and update the value for genericurl value.
	$url = parse_url(curPageURL());
	
	$mxmlproxyurl = $url['scheme'].'://'.$url['host'].($url['port']?":".$url['port']:"").$url['path']."MXMLProxyServer/";
	
	// find the $applicationName.xml 
	$frontendContent = file_get_contents("$applicationName.xml");
	// replace the url with the 	
	$configxml = DOMDocument::loadXML($frontendContent);
	
	$oldmxmlproxyurl = $configxml->getElementsByTagName("genericurl")->item(0)->nodeValue;
	echo "<body>";
	echo "<h1> INSTALL shield </h1> <br />";
	$saveme = false;
	if (isset($_REQUEST["saveme"])) {
		$saveme = true;
	}
	
	// second open the $applicationName.xml located in MXMLProxyServer/
	// find the $applicationName.xml 
	$proxyContent = file_get_contents("MXMLProxyServer/$applicationName.xml");
	// replace the url with the 	
	$proxyxml = DOMDocument::loadXML($proxyContent);
		
	$oldValue = $proxyxml->getElementsByTagName("applicationUri")->item(0)->nodeValue;
	$newurl = $url['scheme'].'://'.$url['host'].($url['port']?":".$url['port']:"").$url['path']."$applicationName/php/";
		
	$oldTemplatePath = $proxyxml->getElementsByTagName("templateLocation")->item(0)->nodeValue;
	$realTemplatePath = realpath("$applicationName/template");
	$realTemplatePath = str_replace( '\\', '/', $realTemplatePath );
	
	$oldApplicationPhpPath = $proxyxml->getElementsByTagName("applicationLocation")->item(0)->nodeValue;
	$realApplicationPhpPath = realpath("$applicationName/php");
	$realApplicationPhpPath = str_replace( '\\', '/', $realApplicationPhpPath );
	
	$arechanges = false;
	if ($oldmxmlproxyurl != $mxmlproxyurl && 
		$oldValue != $newurl && 
		$oldTemplatePath != $realTemplatePath && 
		$oldApplicationPhpPath != $realApplicationPhpPath 
		) {
		
		$arechanges = true;
	}
	if (!$arechanges) {
		echo "<font color=\"#0000FF\">All configuration's are okay, and no changes are required</font><br /><br />";
	}	
	else if (!$saveme) {
		echo "######################################## <br />";
		echo "Changes for $applicationName.xml <br />";
		echo "1- update genericurl value $oldmxmlproxyurl  -> <b>$mxmlproxyurl</b> <br />";
		echo '######################################## <br />';	
	
		echo "######################################## <br />";
		echo "Changes for MXMLProxyServer/$applicationName.xml <br />";
		echo "1- update applicationUri $oldValue  -> <b>$newurl</b> <br />";
		echo "2- update templateLocation $oldTemplatePath  -> <b>$realTemplatePath</b> <br />";
		echo "3- update applicationUri $oldApplicationPhpPath -> <b>$realApplicationPhpPath</b> <br />";
		echo "######################################## <br />";
	}
	$configxml->getElementsByTagName("genericurl")->item(0)->nodeValue = $mxmlproxyurl;
	$proxyxml->getElementsByTagName("applicationUri")->item(0)->nodeValue = $newurl;
	$proxyxml->getElementsByTagName("templateLocation")->item(0)->nodeValue = $realTemplatePath;
	$proxyxml->getElementsByTagName("applicationLocation")->item(0)->nodeValue = $realApplicationPhpPath;
	if ($saveme && $arechanges) {
	    $configxml->save("$applicationName.xml");  
		$proxyxml->save("MXMLProxyServer/$applicationName.xml");  
	}
	
	$currenturl = $url['scheme'].'://'.$url['host'].($url['port']?":".$url['port']:"").$url['path'];
	$applicationurl = "$currenturl$applicationName.html";
	if ($saveme || !$arechanges) {
		echo "To launch the $applicationName demo please press. <a href='$applicationurl'> Launch $applicationName</a><br />";
	} else {
		echo "<form method=\"post\" action=\"setup.php\">";
		echo "<input type=\"hidden\" name=\"saveme\" value=\"true\">";
		echo "<input type=\"submit\" name=\"Submit\" value=\"Install \">";
		echo "</form>";	
	}

?>
</body>
</html>



